import qiniu from 'qiniu'
// 从variable.js中获取accessKey和secretKey和空间名
import variable from './variable.js'
var accessKey = variable.accessKey
var secretKey = variable.secretKey
var bucket = variable.bucket

var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);

// 构建BucketManager对象, 用于资源管理
var config = new qiniu.conf.Config();
//config.useHttpsDomain = true;
config.zone = qiniu.zone.Zone_z0;
var bucketManager = new qiniu.rs.BucketManager(mac, config);


const qiniuTool = {
    getToken() {
        //自定义凭证有效期（示例2小时，expires单位为秒，为上传凭证的有效时间）
        var options = {
            scope: bucket,
            expires: 7200,
            // returnBody: '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}'
        };
        var putPolicy = new qiniu.rs.PutPolicy(options);
        var uploadToken = putPolicy.uploadToken(mac);
        return uploadToken
    },
    getbucketManager() {
        return bucketManager;
    },

    delFile(key) {
        return new Promise((resolve, reject) => {
            bucketManager.delete(bucket, key, function (err, respBody, respInfo) {
                if (err) {
                    reject(err)
                } else {
                    resolve({
                        code:respInfo.statusCode,
                        body:respBody
                    })
                }
            });
        }
        )
    }
}


export default qiniuTool